package com.service;

import com.dao.airticle.ArticleDao;
import com.pojo.Comment;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 处理评论业务
 */
public class CommentService {

    /**
     * 发表评论
     */
    public static Comment write_comment(HttpServletRequest req) throws IOException {
         Comment comment=new Comment();
         String blog_id=req.getParameter("blog_id");
         String user_id=req.getParameter("user_id");
         String comment_content=req.getParameter("comment_content");
         String top_id=req.getParameter("top_id");
         String parent_id=req.getParameter("parent_id");
         String avatar=req.getParameter("avatar");
         Date date = new Date();
         SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
         String create_time=dateFormat.format(date);
         comment.setBlog_id(blog_id);
         comment.setUser_id(user_id);
         comment.setComment_content(comment_content);
         comment.setTop_id(top_id);
         comment.setParent_id(parent_id);
         comment.setCreate_time(create_time);
         comment.setAvatar(avatar);
         String id= ArticleDao.write_comment(comment);
         comment.setId(id);
         return comment;
    }

     /**
      * 获取文章评论
      */
     public static List<Comment> get_article_comment(HttpServletRequest req)throws IOException{
                String blog_id=req.getParameter("blog_id");
                List<Comment>comments=ArticleDao.get_article_comment(blog_id);
               return ArticleDao.get_next_comment(comments);
     }
}
